/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.event;

import com.nus.application.ejb.user.ConferenceEjbLocal;
import com.nus.application.ejb.user.EventEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.dataaccess.dao.conference.Conference;
import com.nus.dataaccess.dao.event.Event;
import com.nus.dataaccess.dao.util.HibernateUtil;
import com.opensymphony.xwork2.ValidationAware;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author
 */
public class EventMaintenance extends BaseAction implements ValidationAware{

    @Inject
    EventEjbLocal eventEjb;

    @Inject
    ConferenceEjbLocal conferenceEjb;

    public String doList(){
        this.validate();
        Map<String , Object> filters = new HashMap<String,Object>();
        filters.put("conferenceId", this.getForm().getEvent().getConferenceId());
        //this.form.setFullListSize(eventEjb.getTotalCountEventByConferenceId(this.getForm().getEvent().getConferenceId(), filters));
        this.form.setFullListSize(eventEjb.getTotalCountEvent(filters));
        this.form.setPageNumber(super.getPage());
        this.getForm().setEvents(
                eventEjb.paginateEvent(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, filters)
                );

        return SUCCESS;
    }

    public String doMaintain(){
        this.validate();

        Event event = eventEjb.retrieveEventById(this.getForm().getEvent().getId());
        this.getForm().setEvent(event);
        //String id = ServletActionContext.getRequest().getParameter("id");
        //this.getForm().setEvent(EventDao.getInstance().getEventById(Long.parseLong(id)));
        return SUCCESS;
    }

    public String doCreate(){
        this.validate();
        //this.getForm().setEvent(new Event());
        return SUCCESS;
    }

    public String doSave(){
        this.validate();

        System.err.print(this.getForm().getEvent().getStartDate());
        Event temp = (Event) HibernateUtil.getCurrentSession().merge(this.getForm().getEvent());
        eventEjb.persistEvent(temp);
        super.addActionMessage("Information successfully saved");
        return SUCCESS;
    }

    private EventMaintenanceForm form = new EventMaintenanceForm();
    public EventMaintenanceForm getForm() {return form;}
    public void setForm(EventMaintenanceForm form) {this.form = form;}

    @Override
    public void validate(){

        if(!super.isSaveEvent()){
            if(super.isListEvent())
                super.clearActionErrors();

            return;
        }

        if(StringUtils.isEmpty(this.getForm().getEvent().getName()))
            super.addActionError(super.getText("error.event_description_is_required"));

        if(!super.hasActionErrors()){


        }
    }

}
